Ext.define('Emp.controller.BaseController', {
    extend: 'Ext.app.Controller',
    
    /**
     * 得到编辑窗口
     * @return Edit
     */
    getEdit: function() {
    	return eval('this.get'.concat(this.getModelName(), 'Edit()'));
    },
    
    /**
     * 得到列表
     * @return List
     */
    getList: function() {
    	return eval('this.get'.concat(this.getModelName(), 'List()'));
    },
    
    /**
     * 得到模型名称
     * @return String
     */
    getModelName: function() {
    	var name = this.$className;
    	return name.substring(name.lastIndexOf('.') + 1, name.length - 10);   	
    },
    
    /**
     * 列表工具栏的新建按钮触发事件方法
     * 打开新建编辑窗口
     */
    doCreateOnList: function() {
    	var edit = this.getEdit();
    	this.filterStatus(edit);
    	this.doBeforeCreateOnList(edit);
    	this.getList().add(edit);
    	edit.show();
    },
    
    /**
     *  列表工具栏的新建按钮触发事件方法时的前置方法, 
     *  主要用于给编辑窗口提供一些参数
     */
    doBeforeCreateOnList: function(edit) {},
    
    filterStatus: function(edit) {
    	var cmps = edit.query('comboboxstatus');
    	Ext.each(cmps, function(cmp) {
    		cmp.getStore().filter('status', 'enable');
    	});
    },
    
    clearFilterStatus: function(edit) {
    	var cmps = edit.query('comboboxstatus');
    	Ext.each(cmps, function(cmp) {
    		cmp.getStore().clearFilter();
    	});
    },
    
    /**
     * 列表工具栏的修改按钮触发事件方法
     * 打开修改编辑窗口
     */
    doModifyOnList: function() {
    	var grid = this.getList().grid;
    	var	record = grid.getSelectionModel().getSelection()[0];
		if (record) {
			var edit = this.getEdit();
    		this.getList().add(edit);
			var id = record.get(record.idProperty);
			edit.pk = id;
			edit.isModify = true;
			edit.show();
		} else {
			Ext.show.msg('提示', '请选择数据');
		}
    },
    
    /**
     * 列表工具栏的查看按钮触发事件方法
     * 打开查看编辑窗口
     */
    doReadOnList: function() {
    	var grid = this.getList().grid;
    	var	record = grid.getSelectionModel().getSelection()[0];
		if (record) {
			var edit = this.getEdit();
			edit.down('splitbutton[action=save]').setVisible(false);
			edit.down('splitbutton[action=create]').setVisible(false);
    		this.getList().add(edit);
			var id = record.get(record.idProperty);
			edit.pk = id;
			edit.isRead = true;
			edit.show();
		} else {
			Ext.show.msg('提示', '请选择数据');
		}
    },
    
    /**
     * 列表工具栏的删除按钮触发事件方法
     * 删除列表选中行
     */
    doRemoveOnList: function() {
    	var me = this;
    	var grid = me.getList().grid;
    	var	records = grid.getSelectionModel().getSelection();
		if (records && records.length > 0) {
			Ext.Msg.show({
				title: '提示',
				msg: '确认删除?',
				buttons: Ext.Msg.YESNOCANCEL,
				fn: function(buttonId, text, opt) {
					if (buttonId === 'yes') {
						var ids = [];
						for(var i = 0; i < records.length; i++) {
							ids.push(records[i].get(records[i].idProperty));
						}
						var opts = {
							method: 'post',
							url: PATH.concat('/',  me.getModelName().toLowerCase(), '/remove'),
							params: {
								id: ids
							},
							success: function(response, options) {
								var result = Ext.decode(response.responseText);
								if (result.success) {
									grid.getStore().remove(records);
									Ext.show.msg('提示', '删除成功!');
								} else {
									Ext.Msg.show({
										title: '提示',
										msg: '删除失败!<br/>' + result.message,
										buttons: Ext.Msg.OK,
										icon: Ext.Msg.ERROR
									});
								}
							},
							failure: function(response, options) {
								Emp.msg.ajaxFailure(response, options)
							}
						};
						Ext.Ajax.request(opts);
					}
				},
				icon: Ext.window.MessageBox.QUESTION
			});
		} else {
			Ext.show.msg('提示', '请选择数据');
		}
    },
    
    /**
     * 编辑窗口保存按钮事件
     */
    doSaveOnEdit: function() {
    	this.doSubmitOnEdit(false);
    },
    
    /**
     * 编辑窗口保存并关闭按钮事件
     */
    doSaveCloseOnEdit: function() {
    	this.doSubmitOnEdit(true);
    },
    
    /**
     * 编辑窗口新建按钮事件
     */
    doCreateOnEdit: function() {
    	var edit = this.getEdit();
    	this.filterStatus(edit);
    	var idProperty = this.getModel(this.getModelName()).prototype.idProperty;
    	edit.setTitle(edit.createTitle);
    	edit.isModify = false;
    	edit.down('form').down('textfield[name=' + idProperty + ']').setReadOnly(false);
    	edit.down('form').getForm().reset();
    },
    
    /**
     * 编辑窗口关闭按钮事件
     * 关闭编辑窗口
     */
    doCloseOnEdit: function() {
    	var edit = this.getEdit();
    	edit.close();
    },
    
    /**
     * 编辑窗口加载数据
     */
    doLoadOnEdit: function() {
    	var me = this;
    	var edit = me.getEdit();
    	if(edit.isModify === true || edit.isRead === true) {
    		var idProperty = me.getModel(me.getModelName()).prototype.idProperty;
    		edit.down('form').down('textfield[name=' + idProperty + ']').setReadOnly(true);
    		if(edit.isModify === true) {
    			edit.setTitle(edit.modifyTitle);
    		} else if(edit.isRead === true) {
    			edit.setTitle(edit.readTitle);
    		}
    		edit.setLoading(true);
			edit.down('form').getForm().load({
				url: PATH.concat('/', me.getModelName().toLowerCase(), '/load/', edit.pk),
				method: 'get',
				success: function(form, action) {
					me.setValues(form, action);
					edit.setLoading(false);
				},
				failure: function(form, action) {
					edit.setLoading(false);
					Emp.msg.formLoadFailure(form, action);
					edit.close();
				}
			});
    	}
    },
    
    /**
     * 编辑窗口form和grid数据填充
     * @param {} form
     * @param {} action
     */
    setValues: function(form, action) {
    	form.setValues(action.result.data);
    },
    
    /**
     * 提交自定义的参数, 主要用于提交表格参数
     * @return {}
     */
    getSubmitParams: function() {
    	return {};
    },
    
    /**
     * 编辑窗口数据校验
     * @return boolean
     */
    isValid: function() {
    	return this.getEdit().down('form').getForm().isValid();
    },
    
    /**
     * 编辑窗口提交数据
     * @param {} close 
     */
    doSubmitOnEdit: function(close) {
    	var me = this;
    	var edit = me.getEdit();
    	if(edit.isRead === true) {
    		return;
    	}
		if (me.isValid()) {
			var operation = 'create';
			if (edit.isModify === true) {
				operation = 'modify';
			}
			edit.down('form').getForm().submit({
				waitTitle: '请稍等',
				waitMsg: '数据保存中...',
				method: 'post',
				url: PATH.concat('/', me.getModelName().toLowerCase(), '/', operation),
				params: me.getSubmitParams(),
				success: function(form, action) {
					Ext.show.msg('提示', '保存成功');
					me.setValues(form, action);
					var data = action.result.data;
					var store = me.getList().grid.getStore();
					if (edit.isModify === true) {
						 var oModel = store.getById(edit.pk);
						 var index = store.indexOfId(edit.pk);
						 //根据主键值创建新的model
						 var nModel = Ext.ModelManager.create(data, oModel.modelName, edit.pk);
						 //删除旧的model
						 store.remove(oModel);
						 //在旧的model位置插入新的model
						 store.insert(index, nModel);
					} else {
						edit.setTitle(edit.modifyTitle);
						edit.isModify = true;
						edit.pk = data[store.model.prototype.idProperty];
						var model = Ext.ModelManager.create(data, store.model.modelName, edit.pk);
						store.insert(0, model);
					}
					if(close === true) {
						edit.close();
					}
				},
				failure: function(form, action) {
					Emp.msg.formSubmitFailure(form, action)
				}
			});
		}
    }
});